# -*- coding: utf-8 -*-
# @Author : Yuan
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict


class PageNum(PageNumberPagination):
    page_size = 5  # 默认每页返回的条数
    page_size_query_param = 'pagesize'
    max_page_size = 20  # 每页返回的最大条数

    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('next', self.get_next_link()),
            ('previous', self.get_previous_link()),
            ('lists', data),
            ('page', self.page.number),  # 页码
            ('pages', self.page.paginator.num_pages),  # 总页数
            ('pagesize', self.page.paginator.per_page),  # 页容量
        ]))


def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'user': user.id,
        'username': user.username,
    }
